const CONTEXT = require('../../utils/public_var.js')
exports.upload = function (that,app) {
  var context = that
  if (app.globalData.OpenID == "" || app.globalData.OpenID == undefined) {
    wx.showModal({
      title: '',
      content: '请您登录验证',
      confirmText: '登录',
      complete: (res) => {
        if (res.cancel) {

        }
        if (res.confirm) {
          wx.redirectTo({
            url: '/pages/login/atLogin/atLogin',
          })
        }
      }
    })
    return
  }
  if (context.data.totalCount > 0) {

    wx.chooseMedia({
      count: 1,
      sizeType: ['compressed'],
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var imgs = res.tempFiles;
        context.setData({
          imgPath: imgs[0].tempFilePath,
          showCrop: false
        })
      },
    })
  } else {
    // 没有免费使用次数了
    wx.showModal({
      title: '体验用尽',
      content: '在下养家糊口，\n恳请主公雇佣在下',
      confirmText: "雇佣",
      cancelText: "取消",
      complete: (res) => {
        if (res.cancel) {}
        if (res.confirm) {
          wx.navigateTo({
            url: '/pages/sita/pay/paybill',
          })
        }
      }
    })
  }
},

exports.initImgMsgData = function(that,data,app){
  var chatMsg =  app.getMsg(that.data.bar_type)
  console.log(chatMsg)
  if (chatMsg != null && chatMsg != undefined && chatMsg.length > 0) {
    that.setData({
      MessageList: chatMsg
    })
    that.setData({
      intoViewName: 'endview'
    })
  }else{
  var initData = [{
    "role": 'system',
    "content": data,
    "msgtype":CONTEXT.TEXT
  }]
  // console.log(initData)
  that.setData({
    MessageList:initData
  })
}
}
exports.editImg = function(that,app,url) {
  var context = that
  wx.editImage({
    src: context.data.imgPath,
    success(res) {
      console.log(res)
     var mathImg = {
        "role": 'user',
        "content": res.tempFilePath,
        "msgtype":CONTEXT.IMAGE 
      }
      var list = context.data.MessageList
      list.push(mathImg)
      context.setData({
        showCrop: true,
        MessageList: list
      })
      context.hintAll()
      context.setData({
        intoViewName: 'endview',
        
      })
     
      wx.showLoading({
        title: '识别中',
      })

      wx.uploadFile({
        url: url,
        filePath: res.tempFilePath,
        name: 'orcimg',
        
        formData: {
          'openid': app.globalData.OpenID
        },
        timeout:60000,
        success: (res) => {
          wx.hideLoading();
          console.log(res);
          let backData =  JSON.parse(res.data);
          console.log(backData);
         if(backData.statusCode==200){
         
           var item = {
            "role": 'system',
            "content": backData.completeOpenAnswer.gptAnswer,
            "msgtype":CONTEXT.TEXT
         
          }
          console.log(item);
          var list = context.data.MessageList
          list.push(item)
          context.setData({
            showCrop: true,
            MessageList: list,
            totalCount:backData.completeOpenAnswer.remainingGPTCount
          })
          app.setCount(backData.completeOpenAnswer.remainingGPTCount)
          context.initSendUI()
          app.saveMsg(that.data.bar_type,list)
          context.setData({
            intoViewName: 'endview'
          })
            
         }
        },
        fail: (res) => {
          wx.hideLoading();
          console.log('fail:' + res);
        },
      });
      
    },
    fail(res) {
       console.log("img")
      console.log(res)
    }
  })
},
 // 显示遮罩层
function showmodels (that) {
  var that = that;
 
},
 // 隐藏遮罩层
 function hideModal (that) {
  var that = that;
  var animation = wx.createAnimation({
    duration: 800, //动画的持续时间 默认400ms   数值越大，动画越慢   数值越小，动画越快
    timingFunction: 'ease', //动画的效果 默认值是linear
  })
  this.animation = animation
  fadeDown(that); //调用隐藏动画   
  setTimeout(function () {
    that.setData({
      showCrop: true
    })
  }, 720) //先执行下滑动画，再隐藏模块

},

//动画集
function  fadeIn(that) {
  var trans = 0 - (wx.getWindowInfo().windowHeight * 0.8 + 54)
  that.animation.translateY(trans).step()

  that.setData({
    animationData: that.animation.export() //动画实例的export方法导出动画数据传递给组件的animation属性
  })
},
function fadeDown (that) {
  that.animation.translateY(wx.getWindowInfo().windowHeight * 0.8).step()
  that.setData({
    animationData: that.animation.export(),
  })
}
